package org.tong.validate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tong.jpa.entities.Student;
import org.tong.validate.req.StudentReq;
import org.tong.validate.services.StudentService;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author
 * @date 2022-08-11 16:26
 */
@RestController
@RequestMapping("/valid")
public class ValidateController {

    @RequestMapping("/")
    public String validate() {
        return "welcome to validate test";
    }


    @RequestMapping("/normal/{name}")
    public String validateS(@PathVariable(name = "name", required = true) String name) {
        return "path variable valid access, var=" + name;
    }

    @RequestMapping("/bean")
    public String validateBean(@Validated @RequestBody StudentReq studentReq) {
        Assert.isNull(studentReq, "不能设置参数");
        return "bean variable access req=" + studentReq.toString();
    }

    @RequestMapping("/list")
    public String validateList(@Valid @RequestBody List<StudentReq> studentReqList) {
        return "list variable access req=" + studentReqList.toString();
    }

    @Resource(name = "validService")
    private StudentService studentService;

    @RequestMapping("/service")
    public String validateService(@RequestBody StudentReq student) {
        return studentService.find(student);
    }
}
